#include "main.hpp"


uint8_t wait_time = 0;
int writeReg(int pt , unsigned char add , char value[])
{
    unsigned char w_buff[2];
    w_buff[0] = (add<<1) + 1;
    w_buff[1] = value[0];
    w_buff[2] = value[1];
    w_buff[3] = value[2];
    w_buff[4] = value[3];
    w_buff[5] = value[4];
    if(write(pt , w_buff , 6) != 6)
    {
        return 1;
    }
    return 0;
    //vector
}

int readReg(int pt , unsigned char s_add , char buf[] , int size)
{
    char writeBuff[1] = {s_add<<1};
    if(write(pt , writeBuff,1) != 1)
    {
        return 1;
    }
    if(read(pt , buf , size) != size)
    {
        return 1;
    }
    return 0;
}

void get_TF_mini_data(void)
{
    char value_for_distanse[5] = {0x5A,0x05,0x00,0x01,0x60};    //cm
    char test[5] = {0x5a,0x05,0x07,0x01,0x67};
    char value_of_distanse[9] = {0};
    uint8_t checksum = 0;
    wait_time++;
    if(wait_time == 1)
    {
        if(writeReg(file,TF_mini_ADD,value_for_distanse) == 1)
        {
            //return 1;
        }
    }
    if(wait_time == 2)
    {
        if(readReg(file,TF_mini_ADD,value_of_distanse,9) == 1)
        {
            //return 1;
        }
        wait_time = 0;
    }
    if((value_of_distanse[0] == 0x59)&&(value_of_distanse[1] == 0x59))
    {
        checksum = (value_of_distanse[0] + value_of_distanse[1] +value_of_distanse[2] +value_of_distanse[3] +value_of_distanse[4] +
                                    value_of_distanse[5] +value_of_distanse[6] +value_of_distanse[7])&0x00FF;
        if(checksum == value_of_distanse[8])
        {
            TF_mini_data = (value_of_distanse[3]<<8) | (value_of_distanse[2]);
            TF_strength = (value_of_distanse[5]<<8) | (value_of_distanse[4]);
        }
        checksum = 0;
    }

    // printf("TF_mini_data = %d\n",TF_mini_data);
    // printf("TF_strength = %d\n",TF_strength);

}
